package com.igeek.compus.backend.system.interceptor;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class ClassLogInterceptor {

    /**
     * 日志记录器
     */
    private final static Logger logger = LogManager.getLogger(ClassLogInterceptor.class.getName());

    /**
     * 无需拦截的方法
     */
    private static final String UN_INERCEPT_METHOD = "toString";

    /**
     * 
     * [描述]:切入点<br>
     * 
     */
    @Pointcut(value = "execution(* com.igeek.compus.backend.*.service..*(..))")
    public void servicePointcut() {

    }

    /**
     * 
     * [描述]:连接点<br>
     * 
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around(value = "servicePointcut()")
    public Object logAround(ProceedingJoinPoint pjp) throws Throwable {
        if (logger.isInfoEnabled() && !UN_INERCEPT_METHOD.equals(pjp.getSignature().getName())) {
            logger.info("enter class[" + pjp.getSignature() + "]");
            Object args[] = pjp.getArgs();
            if (args != null) {
                for (int i = 0; i < args.length; i++) {
                    logger.info("arg" + i + "[" + args[i] + "]      ");
                }
            }
        }

        Object object = null;
        try {
            object = pjp.proceed();
        } catch (Exception e) {
            logger.error("", e);
            throw e;
        }

        if (!UN_INERCEPT_METHOD.equals(pjp.getSignature().getName())
                && logger.isInfoEnabled()) {
            logger.info("exit class[" + pjp.getSignature() + "]");
        }

        if (logger.isInfoEnabled()) {
            logger.info("return object[" + object + "]");
        }

        return object;
    }
}
